﻿@page "/authentications/idproviders/{id}/mappers/{mapperId}"
@layout SimpleIdServer.IdServer.Website.Shared.MainLayout
@attribute [Authorize("Authenticated")]
@inherits FluxorComponent
@using Fluxor.Blazor.Web.Components;
@using SimpleIdServer.IdServer.Api.AuthenticationSchemeProviders;
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Shared.Common
@using SimpleIdServer.IdServer.Website.Stores.IdProviderStore;
@using SimpleIdServer.IdServer.Website.Stores.RealmStore;
@inject IDispatcher dispatcher
@inject NavigationManager navigationManager
@inject NotificationService notificationService
@inject IUrlHelper urlHelper
@inject IState<IdProviderState> idProviderState

<RadzenBreadCrumb>
    <RadzenBreadCrumbItem Path="@urlHelper.GetUrl("/authentications/idproviders")" Text="@Global.IdentityProviders" />
    <RadzenBreadCrumbItem Path=@(urlHelper.GetUrl($"/authentications/idproviders/{id}/mappers")) Text="@Global.IdentityProviderMappers" />
    <RadzenBreadCrumbItem Text="@(mapper?.Name)" />
</RadzenBreadCrumb>

<RadzenText class="mt-3 mb-3" Text="@mapper?.Name" TextStyle="TextStyle.DisplayH3" />
<RadzenCardLoader IsLoading=@idProviderState.Value.IsLoading Variant="Variant.Filled">
    @switch (mapper?.MapperType)
    {
        case MappingRuleTypes.USERATTRIBUTE:
            <SidAuthorizeView Roles="/authentication/manage">
                <Authorized>                    
                    <UserAttributeIdProviderMapper Mapper=mapper AuthProviderMapperUpdated=@UpdateAuthProviderMapper IsEnabled=true></UserAttributeIdProviderMapper>
                </Authorized>
                <NotAuthorized>
                    <UserAttributeIdProviderMapper Mapper=mapper AuthProviderMapperUpdated=@UpdateAuthProviderMapper IsEnabled=false></UserAttributeIdProviderMapper>
                </NotAuthorized>
            </SidAuthorizeView>
            break;
        case MappingRuleTypes.USERPROPERTY:
            <SidAuthorizeView Roles="/authentication/manage">
                <Authorized>                    
                    <UserPropertyIdProviderMapper Mapper=mapper AuthProviderMapperUpdated=@UpdateAuthProviderMapper IsEnabled="true"></UserPropertyIdProviderMapper>
                </Authorized>
                <NotAuthorized>
                    <UserPropertyIdProviderMapper Mapper=mapper AuthProviderMapperUpdated=@UpdateAuthProviderMapper IsEnabled="false"></UserPropertyIdProviderMapper>
                </NotAuthorized>
            </SidAuthorizeView>
            break;
        case MappingRuleTypes.SUBJECT:
        case MappingRuleTypes.IDENTIFIER:
            <SidAuthorizeView Roles="/authentication/manage">
                <Authorized>
                    <UserUniqueIdProviderMapper Mapper=mapper Type=mapper.MapperType AuthProviderMapperUpdated=@UpdateAuthProviderMapper IsEnabled=true></UserUniqueIdProviderMapper>
                </Authorized>
                <NotAuthorized>
                    <UserUniqueIdProviderMapper Mapper=mapper Type=mapper.MapperType AuthProviderMapperUpdated=@UpdateAuthProviderMapper IsEnabled=false></UserUniqueIdProviderMapper>
                </NotAuthorized>
            </SidAuthorizeView>
            break;
    }
</RadzenCardLoader>

@code {
    [Parameter]
    public string id { get; set; }
    [Parameter]
    public string mapperId { get; set; }

    AuthenticationSchemeProviderMapperResult mapper
    {
        get
        {
            return idProviderState.Value.Provider?.Mappers.Single(m => m.Id == mapperId);
        }
    }

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        if(firstRender)
        {
            SubscribeToAction<UpdateAuthenticationSchemeProviderMapperSuccessAction>((act) =>
            {
                notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.IdProviderMapperUpdated });
                StateHasChanged();
            });
        }
    }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if(!string.IsNullOrWhiteSpace(id) && !string.IsNullOrWhiteSpace(mapperId))
        {
            var act = new GetIdProviderAction { Id = id };
            dispatcher.Dispatch(act);
        }
    }

    void UpdateAuthProviderMapper(AuthenticationSchemeProviderMapperResult mapper)
    {
        var act = new UpdateAuthenticationSchemeProviderMapperAction
        {
            Id = mapperId,
            IdProviderName = id,
            Name = mapper.Name,
            SourceClaimName = mapper.SourceClaimName,
            TargetUserAttribute = mapper.TargetUserAttribute,
            TargetUserProperty = mapper.TargetUserProperty
        };
        dispatcher.Dispatch(act);
    }
}